﻿/*******************************************************************************
 * AAEngine
 * Copyright (c) 2010 Mike Jarosch
 *
 * Original source PushButton Engine:
 * Copyright (C) 2009 PushButton Labs, LLC
 * For more information see http://www.pushbuttonengine.com
 *
 * This file is licensed under the terms of the MIT license, which is included
 * in the "MIT License.txt" file at the root directory of this SDK.
 ******************************************************************************/

using Microsoft.Xna.Framework.Audio;
using AAEngine.Engine.Debug;
using AAEngine.Engine.Resources;

namespace AAEngine.Sound
{
    public class SoundHandle : ISoundHandle
    {
        public SoundHandle(SoundManager manager, SoundResource sound, string category, float pan, int loopCount, float startDelay)
        {
            _manager = manager;
            _sound = sound;
            _category = category;
            _pan = pan;
            _loopCount = loopCount;
            _startDelay = startDelay;

            Resume();
        }

        public float Volume
        {
            get { return _volume; }
            set 
            {
                _volume = value;
                IsDirty = true;
            }
        }

        public float Pan
        {
            get { return _pan; }
            set
            {
                _pan = value;
                IsDirty = true;
            }
        }

        public string Category
        {
            get { return _category; }
        }

        public void Pause()
        {
            _instance.Pause();
            _playing = false;
        }

        public void Resume()
        {
            Profiler.Enter("SoundHandle.Resume");

            if (_instance == null)
            {
                IsDirty = true;

                _instance = _sound.Sound.CreateInstance();
                _instance.IsLooped = _loopCount > 0;
                _instance.Play();
                _playing = true;
            }
            else
            {
                _instance.Resume();
            }

            Profiler.Exit("SoundHandle.Resume");
        }

        public void Stop()
        {
            Pause();

            if (_manager.IsInPlayingSounds(this))
            {
                _manager.RemoveSoundHandle(this);
                _instance.Dispose();
                _instance = null;
            }
        }

        public bool IsPlaying
        {
            get { return _playing; }
        }

        internal bool IsDirty { get; set; }

        internal void Update(SoundTransform transform)
        {
            if (_instance != null)
            {
                _instance.Pan = transform.Pan;
                _instance.Volume = transform.Volume;
            }
        }

        internal bool IsDone
        {
            get
            {
                return _instance.State == SoundState.Stopped;
            }
        }

        protected SoundManager _manager;
        protected SoundResource _sound;
        protected string _category;
        protected float _volume = 1;
        protected float _pan;
        protected int _loopCount;
        protected float _startDelay;
        protected SoundEffectInstance _instance;
        protected bool _playing;
    }
}
